﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using He.Common;
using He.Web.UI;
using System.Collections.Generic;
namespace He.Web.manage.administrator
{
    public partial class edit : He.Web.UI.Manage
    {
        public string action = Enums.ActionEnum.Add.ToString(); //操作类型
        private int id = 0;
        public string strType;
        public string strLevel;
        public int AdminType = (int)Enums.AdminType.内容管理员; //默认级别最小
        protected void Page_Load(object sender, EventArgs e)
        {
            string _action = GetString("action");

            if (!string.IsNullOrEmpty(_action) && _action == Enums.ActionEnum.Edit.ToString())
            {
                this.action = Enums.ActionEnum.Edit.ToString();//修改类型
                if (!int.TryParse(Request.QueryString["id"] as string, out this.id))
                {
                    ShowParamError();
                    return;
                }
                this.txtUserName.Enabled = false;
            }
            if (!this.Page.IsPostBack)
            {
                if (Session["AdminType"] != null)
                {
                    AdminType = int.Parse(Session["AdminType"].ToString());
                }
                List<ListItem> TypeList = EnumHelper.GetList(typeof(Enums.AdminType));
                foreach (ListItem item in TypeList)
                {
                    //若自身类型低于对应类型，则不允许选择
                    bool enabled = Convert.ToInt32(item.Value) >= AdminType ? true : false;
                    rblUserType.Items.Add(new ListItem(item.Text, item.Value, enabled));
                }

                if (action == Enums.ActionEnum.Edit.ToString()) //修改
                {
                    chkLoginLevel("editAdministrator");
                    ShowInfo(this.id);
                }
                else
                {
                    chkLoginLevel("addAdministrator");
                }
            }
        }


        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            Model.administrator model = new BLL.administrator().GetModel(_id);
            txtUserName.Text = model.user_name;
            txtUserPwd.Text = model.user_pwd;
            txtRealName.Text = model.real_name;
            txtEmail.Text = model.email;
            rblIsLock.SelectedValue = model.is_lock.ToString();
            rblUserType.SelectedValue = model.user_type.ToString();
            strType = model.user_type.ToString();
            strLevel = model.user_level;
        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            Model.administrator model = new Model.administrator();
            BLL.administrator bll = new BLL.administrator();
            if (bll.Exists(txtUserName.Text))
            {
                JscriptMsg(350, 230, "错误提示", "<b>出现错误了！</b>用户名已存在，请输入别的管理帐号吧！", "", "Error");
                return false;
            }
            model.user_name = txtUserName.Text.Trim();
            model.user_pwd = CryptoHelper.DESEncrypt(txtUserPwd.Text.Trim());
            model.real_name = txtRealName.Text.Trim();
            model.email = txtEmail.Text.Trim();
            model.is_lock = int.Parse(rblIsLock.SelectedValue);
            model.user_type = int.Parse(rblUserType.SelectedValue);
            model.user_level = "," + Request.Form["cbLevel"] + ",";

            if (bll.Add(model) > 0)
            {
                SaveLogs("[管理员管理]增加管理员：" + model.user_name);
                return true;
            }
            return false;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            BLL.administrator bll = new BLL.administrator();
            Model.administrator model = bll.GetModel(this.id);

            //判断是否有修改权限
            int AdminType = Convert.ToInt32(Session["AdminType"]);
            if (AdminType > model.user_type)
            {
                JscriptPrint("您无法修改等级高于您的管理员密码！", "list.aspx", "Error");
                return false;
            }

            model.real_name = txtRealName.Text.Trim();
            model.email = txtEmail.Text.Trim();
            model.is_lock = int.Parse(rblIsLock.SelectedValue);
            model.user_type = int.Parse(rblUserType.SelectedValue);
            model.user_level = "," + Request.Form["cbLevel"] + ",";

            if (bll.Update(model))
            {
                //保存日志
                SaveLogs("[管理员管理]修改管理员：" + model.user_name);
                return true;
            }
            return false;
        }
        #endregion

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string url = CookieHelper.Get("LastListUrl");
            if (string.IsNullOrEmpty(url))
            {
                url = Request.RawUrl;
            }

            if (action == Enums.ActionEnum.Edit.ToString()) //修改
            {
                if (!DoEdit(this.id))
                {
                    JscriptPrint("保存过程中发生错误啦！", url, "Error");
                    return;
                }
                JscriptPrint("修改成功啦！", url, "Success");
            }
            else //添加
            {
                if (!DoAdd())
                {
                    JscriptPrint("保存过程中发生错误啦！", url, "Error");
                    return;
                }
                JscriptPrint("增加成功啦！", url, "Success");
            }
        }
    }
}
